package com.scuec.tourismwebsite.controller;

import com.github.pagehelper.PageInfo;
import com.scuec.tourismwebsite.entity.Admin;
import com.scuec.tourismwebsite.service.AdminService;
import com.scuec.tourismwebsite.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List; // 确保导入了List接口

@RestController
@CrossOrigin
@RequestMapping("tour/v1")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @GetMapping("admin")
    public List<Admin> findAdminAll() {
        return adminService.findAdminAll();
    }

    @GetMapping("admin/{email}")
    public Admin findAdminByEmail(String email) {
        return adminService.findAdminByEmail(email);
    }

    @GetMapping("admins/{email}")
    public List<Admin> findAdminByEm(@PathVariable String email){
        return adminService.findAdminByEm(email);
    }


    @GetMapping("adminpages")
    public PageInfo<Admin> findAdminPages(Integer pageNum, Integer pageSize) {
        return adminService.findAdminPages(pageNum, pageSize);
    }

    @GetMapping("adminpage")
    public PageInfo<Admin> findAdminPage(String nickname, Integer pageNum, Integer pageSize) {
        return adminService.findAdminPage(nickname, pageNum, pageSize);
    }

    @DeleteMapping("admin/{admin_id}")
    public Integer deleteAdmin(@PathVariable Integer admin_id) {
        return adminService.deleteAdminById(admin_id);
    }

    @PostMapping("admin")
    public Integer addAdmin(@RequestBody Admin admin) {
        return adminService.addAdmin(admin);
    }

    @PostMapping("register")
    public String registerAdmin(@RequestBody Admin admin) {
        try {
            Integer result = adminService.addAdmin(admin);
            if (result > 0) {
                return "注册成功";
            } else {
                return "注册失败";
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @PostMapping("login")
    public String loginAdmin(@RequestBody Admin admin) {
        Admin dbAdmin = adminService.findAdminByEmail(admin.getEmail());
        if (dbAdmin != null && dbAdmin.getPassword().equals(admin.getPassword())) {
            return "登录成功";
        } else {
            return "登录失败";
        }
    }
    @PostMapping("upload")    //图片上传
    public String getPicFileName(MultipartFile file) {
        String oldFileName = file.getOriginalFilename();
//        System.out.println(oldFileName);
        String typeName = oldFileName.substring(oldFileName.lastIndexOf("."));
        String filePath= FileUtil.getUploadFilePath();
        String newFileName = System.currentTimeMillis() + typeName;
        System.out.println(newFileName);
        try {
            FileUtil.uploadFile(file.getBytes(),filePath,newFileName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return newFileName;}
}//1




